--http://logging.admicro.vn/
--pv.gif?flashver;java_enabled;screen_resolution;screen_color;timeonpage;urlcrc;hostname;referral_hostname;url_path;sessionid;p1;p2
--http://logging.admicro.vn/pv.gif?11.4.r402;1;1366768;24;10;3523230520;dantri.com.vn;google.com;/default.html;25222522;0;0
--no channelid, geo,

local biz_name = "pv.gif"
-- configure redis connector
if test then
    ConfigRedisConnectors[biz_name]={
        host = test.host,
        port = test.port,
        queue = "k1"
    }
else
    ConfigRedisConnectors[biz_name]={
        host = "192.168.6.97",
        port = 6399,
        queue = "k1"
    }
end

local function pv_gif()
return coroutine.create(function ()
-- constants
local REDIS_SEPERATOR = "\t"
local NUM_SEMICOLON = 11
local DEF_LOCATION = "0"
local lpush_cmd_format = "lpush "..ConfigRedisConnectors[biz_name].queue.." %s "
local cookie_domain = config.http.setcookie.pc

-- headers
local local_config = config
local COOKIE_HEADER = local_config.COOKIE_HEADER
local USER_AGENT_HEADER = local_config.USER_AGENT_HEADER
local SET_COOKIE_HEADER = local_config.SET_COOKIE_HEADER
local REFERER_HEADER = local_config.REFERER_HEADER
local SERVER_HEADER = local_config.SERVER_HEADER
local http_server_name = local_config.http.servername
local PRAGMA_HEADER = local_config.PRAGMA_HEADER
local CACHE_CONTROL_HEADER = local_config.CACHE_CONTROL_HEADER
local EXPIRES_HEADER = local_config.EXPIRES_HEADER
local LAST_MODIFIED_HEADER = local_config.LAST_MODIFIED_HEADER
local CONTENT_TYPE_HEADER = local_config.CONTENT_TYPE_HEADER
local reply_ok = reply.status_type.ok
--cookies
local REGION_COOKIE = local_config.REGION_COOKIE
local GUID_COOKIE = local_config.GUID_COOKIE
local CREATE_COOKIE = local_config.CREATE_COOKIE
local EXPIRE_TIME = local_config.EXPIRE_TIME
local REGION_COOKIE = local_config.REGION_COOKIE
-- functions
local reply_pushHeader = reply.pushHeader
local reply_ok = reply.status_type.ok
local reply_setStatus = reply.setStatus
local request_getHeadersKV = request.getHeadersKV
local request_getParams = request.getParams
local request_getIP = request.getIP
local os_time = os.time
local util_redis_cmd = util.redis_cmd
local util_parseKV = util.parseKV
local util_create_guid = util.create_guid
local util_c_inet_network = util.c_inet_network
local luredis_clear_buffer = luredis.clear_buffer
local luredis_concat_buffer = luredis.concat_buffer
local luredis_send_buffer = luredis.send_buffer
local url_decode = util.fast_url_decode
local l_pushCookieHeader = pushCookieHeader
local coroutine_yield = coroutine.yield

while true do
    local paraTable
    while true do -- validate params
      reply_pushHeader(SERVER_HEADER, http_server_name)
      reply_pushHeader(CONTENT_TYPE_HEADER, "image/gif")
      reply_setStatus(reply_ok)
      paraTable = request_getParams(";");
      if #paraTable == 12 then
        break;
      end
      --print("Invalid params", #paraTable)
      coroutine_yield(); -- return due to incorrect number of params
    end
    -- table
    local headerTable = request_getHeadersKV();
    local user_agent = headerTable[USER_AGENT_HEADER]
    local referer = headerTable[REFERER_HEADER];
    -- cookie
    local cookieTable = util_parseKV(headerTable[COOKIE_HEADER],";=");
    -- Get guid: guid
    local guid = cookieTable[GUID_COOKIE];
    local create_cookie = cookieTable[CREATE_COOKIE]

    local create_time = os_time();
    local ip = request_getIP()
    local expire_time = EXPIRE_TIME+create_time;

    -- guid
    if not guid then
        guid = util_create_guid(create_time, ip);
        if guid then
            create_cookie = create_time
            local expire_time = EXPIRE_TIME+create_time;
            l_pushCookieHeader(GUID_COOKIE, guid, expire_time, "/", cookie_domain, false)
            l_pushCookieHeader(CREATE_COOKIE, create_time, expire_time, "/", cookie_domain, false)
        else
            guid = "0"
        end
        newguid="1"
    end
    if not create_cookie then
        create_cookie = create_time
            l_pushCookieHeader(CREATE_COOKIE, create_time, expire_time, "/", cookie_domain, false)
    end

    -- flashver;java_enabled;screen_resolution;screen_color;
    -- timeonpage;urlcrc;hostname;referral_hostname;url_path
    --[[local fl = paraTable[1]
    local je = paraTable[2]
    local sr = paraTable[3]
    local sc = paraTable[4]
    local top = paraTable[5] -- time on page
    local urlcrc = paraTable[6]
    local hn = paraTable[7]
    local ref_hn = paraTable[8]
    local url_path = paraTable[9]
    local sid = paraTable[10]
    local p1 = paraTable[11]
    local p2 = paraTable[12]]
    ---------
    luredis_clear_buffer()
    luredis_concat_buffer(REDIS_SEPERATOR,
        create_time, create_cookie, user_agent, referer, guid,
        table.concat(paraTable,REDIS_SEPERATOR))
    util_redis_cmd(biz_name,true,luredis_send_buffer,lpush_cmd_format);
    coroutine_yield()
end
end)
end

creators[biz_name] = function()
    f[biz_name] = pv_gif()
end
